package com.faxsun.web.utils;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class MobileDetect {

    public static String getProperView(HttpServletRequest request, String mobileView, String desktopView) {
        Device device = DeviceUtils.getCurrentDevice(request);
        if (device.isMobile()) {
            return mobileView;
        } else {

            return desktopView;
        }

    }
    
    /**
     * @return {@link HttpServletRequest}
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest ret = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return ret;
    }

    public static long getFingerPrint(HttpServletRequest request) {
        String followerIdStr = request.getParameter("followerId");// follower id
        long followerId = Long.parseLong(followerIdStr);
        String ip = MobileDetect.getClientIp(request);
        long ipLong = MobileDetect.ipToLong(ip);
        return followerId + ipLong;
    }

    public static String getClientIp(HttpServletRequest request) {
        String xForward = request.getHeader("x-forwarded-for");

        if (xForward == null || xForward.length() == 0 || xForward.equalsIgnoreCase("unknown")) {
            String proxyClientIP = request.getHeader("Proxy-Client-IP");
            if (proxyClientIP == null || proxyClientIP.length() == 0 || "unknown".equalsIgnoreCase(proxyClientIP)) {
                String wlProxyClientIP = request.getHeader("WL-Proxy-Client-IP");
                if (wlProxyClientIP == null || wlProxyClientIP.length() == 0
                        || "unknown".equalsIgnoreCase(wlProxyClientIP)) {
                    return request.getRemoteAddr();
                }
                return wlProxyClientIP;
            }
            return proxyClientIP;
        }
        return xForward;
    }

    public static long ipToLong(String ipAddress) {

        long result = 0;

        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByName(ipAddress);
            if (inetAddress instanceof Inet6Address) {
                String[] addrArray = ipAddress.split(":");
                long[] num = new long[addrArray.length];

                for (int i = 0; i < addrArray.length; i++) {
                    num[i] = Long.parseLong(addrArray[i], 16);
                }
                long long1 = num[0];
                for (int i = 1; i < 4; i++) {
                    long1 = (long1 << 16) + num[i];
                }
                long long2 = num[4];
                for (int i = 5; i < 8; i++) {
                    long2 = (long2 << 16) + num[i];
                }

                long[] longs = { long2, long1 };
                result = longs[0] + longs[1];// simply add up
            } else {
                String[] ipAddressInArray = ipAddress.split("\\.");
                for (int i = 3; i >= 0; i--) {
                    long ip = Long.parseLong(ipAddressInArray[3 - i]);
                    // left shifting 24,16,8,0 and bitwise OR
                    // 1. 192 << 24
                    // 1. 168 << 16
                    // 1. 1 << 8
                    // 1. 2 << 0
                    result |= ip << (i * 8);
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return result;
    }

}
